from sys import stdin, stdout
def get_ints(): return map(int, stdin.readline().split())
def PRINT(s): stdout.write(s + '\n')
for w in range(int(stdin.readline())):
n = int(stdin.readline())
A = []
B = []
for i in range(n):
A.append([int(x) for x in list(stdin.readline()[:-1])])
stdin.readline()
for i in range(n):
B.append([int(x) for x in list(stdin.readline()[:-1])])
for i in range(n):
if A[i][0] != B[i][0]:
for j in range(n):
A[i][j] = 1 - A[i][j]
for j in range(n):
if A[0][j] != B[0][j]:
for i in range(n):
A[i][j] = 1 - A[i][j]
PRINT(["NO","YES"][A==B])
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define ppb pop_back
#define int long long
#define itn long long
#define endl "\n"
#define fi first
#define se second
#define forf(i, a, b) for (int i = (a); i < (b); i++)
#define forr(i, a, b) for (int i = (a); i >=(b); i -= 1)
#define prdouble(x) cout<< fixed << setprecision(10)<< x;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<pair<int,int>> vpii;
typedef pair<ll, ll> pll;
typedef unordered_map<int,int> umap;
#define yeah cout<<"YES"<<endl;
#define nope cout<<"NO"<<endl;
#define all(v) v.begin(),v.end()
#define print(vec, l, r) \
for (int i = l; i < r; i++) \
cout << vec[i] << " "; \
cout << endl;
#define debug(v) cout<<v<<endl;
const int N=1e5+5;
const ll mod = 1e9 + 7;
const ll inf = 1e17;
long long ceil(int n,int i)
{
return (n+i-1)/i;
}
long long power(long long a,long long b,long long m=mod){
long long p=1;
a%=m;
while(b){
if(b&1) p=(p*1LL*a)%m;
a=(a*1LL*a)%m;
b>>=1;
}
p%=m;
return p;
}
void solve()
{
int n;
cin>>n;
vvi a(n);
vvi b(n);
forf(i,0,n)
{
string s;
cin>>s;
forf(j,0,n)
{
if(s[j]=='1') a[i].pb(1);
else a[i].pb(0);
}
}
forf(i,0,n)
{
string s;
cin>>s;
forf(j,0,n)
{
if(s[j]=='1') b[i].pb(1);
else b[i].pb(0);
}
}
vvi temp(n);
forf(i,0,n)
{
forf(j,0,n)
{
if(a[i][j]!=b[i][j]) temp[i].pb(1);
else temp[i].pb(0);
}
}
int k=-1;
set<int> row,col;
forf(i,0,n)
{
forf(j,0,n)
{
if(temp[i][j])
{
k=i;
row.insert(k);
break;
}
}
if(k!=(-1))
{
forf(j,0,n)
{
if(temp[i][j]==0)
{
col.insert(j);
}
}
break;
}
}
forf(i,0,n)
{
if(i==k) continue;
forf(j,0,n)
{
if(col.find(j)!=col.end())
{
if(temp[i][j]==0)
{
row.insert(i);
break;
}
}
else
{
if(temp[i][j])
{
row.insert(i);
break;
}
}
}
}
// for(auto it:row)
// {
// cout<<it<<endl;
// }
// cout<<"col"<<endl;
// for(auto it:col)
// {
// debug(it);
// }
int flag=1;
forf(i,0,n)
{
int c=row.find(i)!=row.end();
forf(j,0,n)
{
int d=col.find(j)!=col.end();
int req=temp[i][j]%2;
int have=(c+d)%2;
if(req!=have)
{
flag=0;
break;
}
}
if(flag==0) break;
}
if(flag)
{
yeah
return;
}
k=-1;
row.clear();
col.clear();
forf(j,0,n)
{
forf(i,0,n)
{
if(temp[i][j])
{
k=j;
col.insert(j);
break;
}
}
if(k!=(-1))
{
forf(i,0,n)
{
if(temp[i][j]==0)
{
row.insert(i);
}
}
break;
}
}
forf(i,0,n)
{
if(i==k) continue;
forf(j,0,n)
{
if(row.find(j)!=row.end())
{
if(temp[j][i]==0)
{
col.insert(j);
break;
}
}
else
{
if(temp[j][i])
{
col.insert(j);
break;
}
}
}
}
flag=1;
forf(i,0,n)
{
int c=row.find(i)!=row.end();
forf(j,0,n)
{
int d=col.find(j)!=col.end();
int req=temp[i][j]%2;
int have=(c+d)%2;
if(req!=have)
{
flag=0;
break;
}
}
if(flag==0) break;
}
if(flag) yeah
else nope;
return;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t=1;
cin>>t;
while(t--)
{
// cout<<t<<" ";
solve();
}
}
742A - Arpa’s hard exam and Mehrdad’s naive cheat | 1492A - Three swimmers |
1360E - Polygon | 1517D - Explorer Space |
1230B - Ania and Minimizing | 1201A - Important Exam |
676A - Nicholas and Permutation | 431A - Black Square |
474B - Worms | 987B - High School Become Human |
1223A - CME | 1658B - Marin and Anti-coprime Permutation |
14B - Young Photographer | 143A - Help Vasilisa the Wise 2 |
320A - Magic Numbers | 1658A - Marin and Photoshoot |
514A - Chewbaсca and Number | 382A - Ksenia and Pan Scales |
734B - Anton and Digits | 1080A - Petya and Origami |
1642D - Repetitions Decoding | 1440A - Buy the String |
1658F - Juju and Binary String | 478A - Initial Bet |
981A - Antipalindrome | 365A - Good Number |
1204B - Mislove Has Lost an Array | 1409D - Decrease the Sum of Digits |
1476E - Pattern Matching | 1107A - Digits Sequence Dividing |